package com.utils;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import android.util.DisplayMetrics;

import com.bank.Bank;
import com.receive.SMSReceiverSend;
import com.show.api.util.StringUtils;

import static android.os.SystemClock.sleep;
import static com.bank.Bank.smsCode;
import static com.bank.Bank.smsCode_86;

/**
 * 项目名称：yzf_jiao
 * 类描述：代码封装
 * 创建人：Administrator
 * 创建时间：2020/4/2 9:29
 * 修改人：Administrator
 * 修改时间：2020/4/2 9:29
 * 修改备注：
 */
public class CodeUtils {
    static Bank bankInfo;

    /**
     * 进入APP
     *
     * @return
     * @throws UiObjectNotFoundException
     */
    public static void initData(Bank bank) throws UiObjectNotFoundException {
        LogToFile.init(InstrumentationRegistry.getTargetContext(), Constants.PROT);
        bank.mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        if (!android.os.Build.MODEL.equals("SM-C710F")) {
            UIUtils.initServicePermission(bank.mDevice);
        }
        bank.mDevice.pressHome();
        bank.mDevice.pressHome();
        sleep(ChatClient.overtime_1);
        getScreenInfo(bank);
        Constants.tryingToReadAccountInformation(bank.mDevice, bankInfo.BANK_NAME);
        //启动银行app
        UiObject x = bank.mDevice.findObject(new UiSelector().text(bankInfo.BANK_NAME));
        x.click();
        Constants.CODE = "";
        showMsg(bankInfo.BANK_NAME, "进入" + bankInfo.BANK_NAME + "银行");
    }

    public static void getScreenInfo(Bank bank) {
        bank.context = InstrumentationRegistry.getTargetContext();
        DisplayMetrics dm = bank.context.getResources().getDisplayMetrics();
        bank.screenWidth = dm.widthPixels;
        bank.screenHeight = dm.heightPixels;
        bank.ratio = bank.screenWidth / 720f;
        bank.ratioH = bank.screenHeight / 1080f;
        bankInfo = bank;
        bank.keyPhone(bank.screenHeight);
    }


    public static void initSMSReceiver(Context context) {
        Constants.MOBILE_BOB = smsCode;
        Constants.MOBILE_BOB_86 = smsCode_86;
        SMSReceiverSend sMSReceiverSend = new SMSReceiverSend();
        Constants.CODE = "";
        IntentFilter filter = new IntentFilter();
        filter.addAction(Constants.SMS_ACTION);
        filter.addAction("net.blogjava.mobile.SMSto");
        // 设置优先级 不然监听不到短信
        filter.setPriority(ChatClient.overtime_1);
        context.registerReceiver(sMSReceiverSend, filter);
        Intent intent = new Intent();
        intent.setAction("com.tiantian.SEND_TO");
        context.sendBroadcast(intent);
        //动态注册sub连接状态广播
        ImageView.saveImg("");
    }

    /**
     * @param message   内容
     * @param className 类名
     * @return
     */
    public static UiObject getUiObject_textOrClass(String message, String className) {
        UiObject mV = bankInfo.mDevice.findObject(new UiSelector().text(message).className(className));
        if (!mV.exists()) {
            mV = bankInfo.mDevice.findObject(new UiSelector().descriptionContains(message).className(className));
        }
        return mV;
    }

    /**
     * @param message 内容
     * @param id
     * @return mV
     */
    public static UiObject getUiObject_textOrId(String message, String id) {
        UiObject mV = bankInfo.mDevice.findObject(new UiSelector().text(message).resourceId(id));
        if (!mV.exists()) {
            mV = bankInfo.mDevice.findObject(new UiSelector().descriptionContains(message).resourceId(id));
        }
        return mV;
    }

    /**
     *
     * @param message 内容
     * @param index
     * @return
     */
    public static UiObject getUiObject_textOrIndex(String message, int index) {
        UiObject mV = bankInfo.mDevice.findObject(new UiSelector().text(message).index(index));
        if (!mV.exists()) {
            mV = bankInfo.mDevice.findObject(new UiSelector().descriptionContains(message).index(index));
        }
        return mV;
    }

    /**
     * @param message     内容
     * @param classString
     * @return mV
     */
    public static UiObject getUiObject_containTextOrClass(String message, String classString) {
        UiObject mV = bankInfo.mDevice.findObject(new UiSelector().textContains(message).className(classString));
        if (!mV.exists()) {
            mV = bankInfo.mDevice.findObject(new UiSelector().descriptionContains(message).className(classString));
        }
        return mV;
    }

    public static String getUiObject_id_text(String id) throws UiObjectNotFoundException {
        String s = null;
        UiObject mTV = bankInfo.mDevice.findObject(new UiSelector().resourceId(id));
        if (mTV.exists()) {
            s = mTV.getText();
            if (StringUtils.isEmpty(s)) {
                s = mTV.getContentDescription();
            }
        }
        return s;
    }

    /**
     * 获取View文字内容
     *
     * @param className
     * @param id
     * @return
     * @throws UiObjectNotFoundException
     */
    public static String getUiObject_text(String className, String id) throws UiObjectNotFoundException {
        String s = null;
        UiObject mTV = bankInfo.mDevice.findObject(new UiSelector().className(className).resourceId(id));
        if (mTV.exists()) {
            s = mTV.getText();
            if (StringUtils.isEmpty(s)) {
                s = mTV.getContentDescription();
            }
        }
        return s;
    }

    public static UiObject getUiObject_contentText(String msg) {
        UiObject mTV = bankInfo.mDevice.findObject(new UiSelector().textContains(msg));
        if (!mTV.exists()) {
            mTV = bankInfo.mDevice.findObject(new UiSelector().descriptionContains(msg));
        }
        return mTV;
    }

    public static String getUiObject_content(UiObject uiObject) {
        String content = null;
        try {
            content = uiObject.getText();
            if (StringUtils.isEmpty(content)) {
                content = uiObject.getContentDescription();
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
        return content;
    }

    /**
     * @param oldTime  起始时间
     * @param overTime 结束时间值
     * @param message  超时信息
     * @param isReport 是否上报
     * @return outTime
     */
    public static boolean outTime(long oldTime, int overTime, String message, boolean isReport) {
        long nowtime = System.currentTimeMillis();
        if ((nowtime - oldTime) > overTime) {
            showMsg(bankInfo.BANK_NAME, message);
            if (isReport) {
                ChatClient.timer(LogToFile.getFatherMethodName(), message);
            }
            return true;
        }
        return false;
    }

    public static void showMsg(String BANK_NAME, String msg) {
        LogToFile.d(BANK_NAME, msg);
    }
}
